<!--
 * @Description: 评审意见
 * @Author: wind-lc
 * @version: 1.0
 * @Date: 2023-02-07 10:30:37
 * @LastEditTime: 2023-03-13 15:21:13
 * @FilePath: \cscec-pms-admin\src\views\project\projectPlanningMeeting\components\ReviewerComment.vue
-->
<template>
  <BaseModal
    :visible="visible"
    title="评审意见"
    :width="1200"
    destroy-on-close
    :wrap-class-name="'node-maintain'"
    @cancel="handleModalCancel()"
  >
    <custom-form
      ref="form"
      :is-validate="true"
      :edit-model="''"
      :form="form"
      :form-list="formOption"
      :is-emit="false"
    >
      <UploadList
        :disabled="reviewData.type === 'detail'"
        slot="fileInfoList"
        :businessCode="'PM46'"
        :uploadArrys.sync="form.fileInfoList"
        multiple
      />
    </custom-form>
    <template slot="footer">
      <a-button @click="handleModalCancel()">
        关闭
      </a-button>
      <a-button
        v-if="reviewData.type !== 'detail'"
        type="primary"
        :loading="submiting"
        @click="handleSubmit"
      >
        提交
      </a-button>
    </template>
  </BaseModal>
</template>
<script>
import BaseModal from '@/components/BaseModal'
import { formOption2 } from '../model.js'
import UploadList from '@/components/UploadList'
import training from '@/api/design/training'
export default {
  name: 'ReviewerComment',
  components: {
    BaseModal,
    UploadList
  },
  props: {
    // 隐藏显示
    visible: {
      type: Boolean,
      require: true
    },
    // 模态框数据
    reviewData: {
      type: Object,
      default: () => {
        return {
          type: 'add',
          data: {}
        }
      }
    },
    // 会议id
    meetingId: {
      type: String,
      require: ''
    }
  },
  data() {
    return {
      // 提交中
      submiting: false,
      //表单
      form: {}
    }
  },
  computed:{
    // 表单项
    formOption(){
      return this.reviewData.type === 'detail' ? formOption2.map(el=>{
        const item = this.$clone(el)
        if(el.keyName === 'reviewComments'){
          item.disabled = true
        }
        return item
      }) : formOption2
    }
  },
  watch: {
    // 监听显示隐藏
    visible: {
      handler(val) {
        if (val) {
          switch(this.reviewData.type){
            case 'add':
              // 新增
              this.form = {
                creatorName: this.$store.state.user.userInfo.name,
                createTime: this.moment().format('YYYY-MM-DD HH:mm:ss'),
                fileInfoList: []
              }
              break
            case 'detail':
              // 查看
              this.form = this.reviewData.data
              break
            case 'update':
              // 编辑
              this.form = this.reviewData.data
              break
          }
        }else{
          this.form = {}
        }
      }
    }
  },
  methods: {
    /**
     * @description: 提交
     * @return {void}
     */
    handleSubmit() {
      const { reviewComments, fileInfoList } = this.form
      if((reviewComments??'') === '' && fileInfoList?.length < 1){
        this.$message.warning('请至少填写评审意见与附件中的一项')
      }else{
        // this.submiting = true
        this.$confirm({
          title: '提示',
          content: '请确认是否要提交评审意见？提交后不可再次修改',
          onOk: () => {
            return this.reviewData.type === 'add' ? this.add(reviewComments, fileInfoList) : this.update(reviewComments, fileInfoList)
          }
        })
        
      }
    },
    /**
     * @description: 新增
     * @param {string} reviewComments 评审意见
     * @param {array} fileInfoList 附件列表
     * @return {void}
     */    
    add(reviewComments, fileInfoList){
      return training.addReviewComments({
        meetingId: this.meetingId,
        reviewComments,
        fileInfoList: fileInfoList.map((el) => {
          const item = this.$clone(el)
          delete item.id
          return item
        })
      }).then(res=>{
        if(res.code === this.SUCCESS_CODE){
          this.$message.success('新增成功')
          this.handleModalCancel(true)
        }
      }).finally(() => {
        this.submiting = false
      })
    },
    /**
     * @description: 编辑
     * @param {string} reviewComments 评审意见
     * @param {array} fileInfoList 附件列表
     * @return {void}
     */    
    update(reviewComments, fileInfoList){
      return training.editReviewComments({
        id: this.reviewData.data.id,
        meetingId: this.meetingId,
        reviewComments,
        fileInfoList: fileInfoList.map((el) => {
          const item = this.$clone(el)
          delete item.id
          return item
        })
      }).then(res=>{
        if(res.code === this.SUCCESS_CODE){
          this.$message.success('编辑成功')
          this.handleModalCancel(true)
        }
      }).finally(() => {
        this.submiting = false
      })
    },
    /**
     * @description: 关闭模态框
     * @param {boolean} 是否需要刷新
     * @return {void}
     */
    handleModalCancel(refresh = false) {
      this.$emit('update:visible', false)
      if (refresh) {
        this.$emit('refresh', true)
      }
    }
  }
}
</script>
<style lang="less" scoped>
</style>
